.SUFFIXES:
.SUFFIXES: .F90 .f90 .o .a .c .cu

# Include definitions

include ./paths.mk
include ./include.mk

# Compiler commands.

INCLUDES   = -I$(OUTILS) $(PAR_INCS) $(HDF5_INCS) $(NETCDF_INCS) $(CUDA_INCS)
LIBS       = $(CUDA_LIBS)
F_COMMAND  = $(F_COMP) -c $(F_OPTS) $(INCLUDES)
C_COMMAND  = $(C_COMP) -c $(C_OPTS) $(INCLUDES) -D$(CMACH)
CUDA_COMMAND = $(CUDA_COMP) ./libcutil_x86_64.a -c $(CUDA_OPTS)

ifndef F_DEFINE_FLAG
F_DEFINE_FLAG=-D
endif

ifdef CMACH
MACH_DEF=$(F_DEFINE_FLAG)$(CMACH)
endif

ifdef OLAM_MPI
MPI_DEF=$(F_DEFINE_FLAG)OLAM_MPI
OLAM_SUFFIX=-mpi
endif

# Define archive and executable names.

BASE=olam
EXE=$(BASE)-$(OLAM_VERSION)$(OLAM_SUFFIX)
ARC=$(BASE)-$(OLAM_VERSION).a

# OLAM source files

include objects.mk

# cu rule.

.cu.a:
	@echo ""
	$(CUDA_COMMAND) $<
	$(ARCHIVE) $@ $(<F:.cu=.o)
	rm -f $(<F:.cu=.o)

# f90 rule.

.f90.a:
	@echo ""
	$(F_COMMAND) $<
	$(ARCHIVE) $@ $(<F:.f90=.o)
	rm -f $(<F:.f90=.o)

# F90 rule.

.F90.a:
	@echo ""
	$(F_COMMAND) $(MACH_DEF) $(MPI_DEF) $<
	$(ARCHIVE) $@ $(<F:.F90=.o)
	rm -f $(<F:.F90=.o)

# c rule.

.c.a:
	@echo ""
	$(C_COMMAND) $<
	$(ARCHIVE) $@ $(<F:.c=.o)
	rm -f $(<F:.c=.o)


# Define targets.

all: $(EXE)

MAIN_OBJ = $(notdir $(MAIN:.f90=.o))
MPIF_OBJ = $(notdir $(MPIF:.F90=.o))

$(EXE): $(ARC) $(MPIF) $(MAIN) FORCE
	@echo ""
	$(LOADER) -o $(EXE) $(MAIN_OBJ) $(MPIF_OBJ) $(LOADER_OPTS) $(ARC) \
	    $(LIBNCARG) $(PAR_LIBS) $(LIBS) $(HDF5_LIBS) $(NETCDF_LIBS)
	rm -f *.o
	@echo ""
	@echo Finished building === $(EXE)
	@echo ""

$(MAIN): FORCE
	@echo ""
	$(F_COMMAND) $@

$(MPIF): FORCE
	@echo ""
	$(F_COMMAND) $(MACH_DEF) $(MPI_DEF) $@

$(ARC): $(OBJ)

FORCE:

clean:
	@echo ""
	rm -f $(ARC) $(BASE)-$(OLAM_VERSION) $(BASE)-$(OLAM_VERSION)-mpi
	rm -f *.o *.mod *.f *.stb
	@echo ""

# Include dependencies (if this does not work, try using gnu make or copy the
# contents of the file dep_rams here).

include dep_olam.mk
